package com.ddh.service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddh.service.common.BaseEntity;
import com.ddh.service.domain.game.dto.GameDTO;
import com.ddh.service.domain.game.dto.GamePageDTO;
import com.ddh.service.domain.game.entity.Games;
import com.ddh.service.mapper.GamesMapper;
import com.ddh.service.service.IGamesService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ddh
 * @since 2025-01-16
 */
@Service
public class GamesServiceImpl extends ServiceImpl<GamesMapper, Games> implements IGamesService {

    @Resource
    private GamesMapper gamesMapper;

    /**
     * 添加游戏 (需要做权限校验的，只有什么身份的人或者游戏段位高的人才能进行出题目)
     * @return
     */
    @Override
    public Boolean addGame(GameDTO gameDTO) {
        Games games = new Games();
        //将gameDTO属性和值拷贝到Games对象中
        BeanUtils.copyProperties(gameDTO,games);
        games.setCreateAt(LocalDateTime.now());
        games.setUpdateAt(LocalDateTime.now());
        return this.save(games);
    }

    /**
     * 分页查询游戏
     * @return
     */
    @Override
    public BaseEntity<Games> selectPageGame(GamePageDTO gamePageDTO) {
        LambdaQueryWrapper<Games> wrapper = new LambdaQueryWrapper<Games>()
                .eq(Games::getCategory, gamePageDTO.getCategory());
        Page page=new Page(gamePageDTO.getPageNo(),gamePageDTO.getPageSize());
        IPage iPage = gamesMapper.selectPage(page, wrapper);
        BaseEntity<Games> gamesBaseEntity = new BaseEntity<>();
        gamesBaseEntity.setRecords(iPage.getRecords());
        gamesBaseEntity.setTotal(iPage.getTotal());
        return gamesBaseEntity;
    }
}
